package com.py.credit.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.py.credit.common.ServiceException;
import com.py.credit.dao.SharingMetricInfoMapper;
import com.py.credit.dao.SharingMetricMapper;
import com.py.credit.entity.Column;
import com.py.credit.entity.SharingMetric;
import com.py.credit.entity.SharingMetricInfo;
import com.py.credit.service.SharingMetricService;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;

@Service
public class SharingMetricServiceImpl implements SharingMetricService {

	@Autowired
	private SharingMetricMapper sharingMetricMapper;

	@Autowired
	private SharingMetricInfoMapper sharingMetricInfoMapper;

	@Override
	public List<Column> getSharingColumns(String kId) {
		return sharingMetricMapper.selectSharingColumns(kId);

	}

	@Override
	public List<Map<String, Object>> getByConditionForPage(List<String> showColumns, String[] additionalColumns,
			String condition, Page page) {

		if (CollectionUtils.isEmpty(showColumns)) {

			throw new ServiceException("至少选择一列");

		}
		List<String> conditions = SQLBuilder.createConditionSql(condition);

		return sharingMetricMapper.selectTableListByConditionForPage(showColumns, additionalColumns, conditions, page);

	}

	@Override
	public void add(SharingMetricInfo sharingMetricInfo) {

		sharingMetricInfoMapper.insertSelective(sharingMetricInfo);

	}

	@Override
	public void addColumns(int id, List<SharingMetric> columns) {
		sharingMetricMapper.add(id, columns);

	}

	@Override
	public Integer getCountByKId(Integer kId) {

		return sharingMetricMapper.selectCountByKId(kId);
	}

	@Override
	public void del(String ids) {

		String[] idArray = ids.split(",");

		for (String id : idArray) {

			sharingMetricInfoMapper.deleteByPrimaryKey(Integer.valueOf(id));

		}

	}

	@Override
	public List<Column> getSharingMetricByKid(String kId) {

		return sharingMetricMapper.selectSharingColumns(kId);
	}

	@Override
	public SharingMetricInfo getMetricInfoById(String id) {

		return sharingMetricInfoMapper.selectByPrimaryKey(Integer.valueOf(id));
	}

	@Override
	public void updateDbMetrics(int infoId, List<SharingMetric> addColumns, List<SharingMetric> updateColumns,
			List<SharingMetric> delColumns) {

		if (!CollectionUtils.isEmpty(addColumns)) {

			for (SharingMetric addColumn : addColumns) {

				if (StringUtils.isBlank(addColumn.getMetricName())) {

					throw new ServiceException("数据项名称不能为空");
				}
				if (StringUtils.isBlank(addColumn.getMetricCode())) {

					throw new ServiceException("数据项编码不能为空");
				}

				if (StringUtils.isBlank(addColumn.getFieldType())) {

					throw new ServiceException("数据项类型不能为空,数据项名称:" + addColumn.getMetricName());
				}

				if ("number".equals(addColumn.getFieldType()) && (addColumn.getFieldLength() == null
						|| addColumn.getFieldLength() > 63 || addColumn.getFieldLength() <= 0)) {

					throw new ServiceException("数字类型长度范围1~63");
				}

				if ("varchar".equals(addColumn.getFieldType()) && (addColumn.getFieldLength() == null
						|| addColumn.getFieldLength() > 4000 || addColumn.getFieldLength() <= 0)) {

					throw new ServiceException("字符串类型长度范围1~4000");
				}

			}

			sharingMetricMapper.add(infoId, addColumns);

		}

		if (!CollectionUtils.isEmpty(updateColumns)) {

			for (SharingMetric column : updateColumns) {

				if (StringUtils.isBlank(column.getMetricName())) {

					throw new ServiceException("数据项名称不能为空");
				}
				if (StringUtils.isBlank(column.getMetricCode())) {

					throw new ServiceException("数据项编码不能为空");
				}

				if (StringUtils.isBlank(column.getFieldType())) {

					throw new ServiceException("数据项类型不能为空,数据项名称:" + column.getMetricName());
				}

				if ("number".equals(column.getFieldType()) && (column.getFieldLength() == null
						|| column.getFieldLength() > 63 || column.getFieldLength() <= 0)) {

					throw new ServiceException("数字类型长度范围1~63");
				}

				if ("varchar".equals(column.getFieldType()) && (column.getFieldLength() == null
						|| column.getFieldLength() > 4000 || column.getFieldLength() <= 0)) {

					throw new ServiceException("字符串类型长度范围1~4000");
				}

				sharingMetricMapper.updateByPrimaryKeySelective(column);
			}

		}

		if (!CollectionUtils.isEmpty(delColumns)) {

			if (!CollectionUtils.isEmpty(delColumns)) {

				sharingMetricMapper.delColumns(delColumns);

			}

		}

	}
}
